﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using MensUnoShop.Areas.Admin.Models;
using MensUnoShop.Models;
using SenerPro;
using MvcPaging;
using MensUnoShop.Areas.Admin.Filters;


namespace MensUnoShop.Areas.Admin.Controllers
{
    [ManagerCheckFilter]
    public class NewsSetupController : Controller
    {
        //
        // GET: /Admin/NewsSetup/
        int defaultPageSize = 30;
        public ActionResult Index(int? page)
        {
            int currentPageIndex = page.HasValue ? page.Value - 1 : 0;
            List<UNews> nlist = NewsFactory.GetNewsListAll();
            return View(nlist.ToPagedList(currentPageIndex, defaultPageSize));
        }

        [HttpPost]
        public ActionResult Index(int nid, string act)
        {
            if(act=="upt")
                NewsFactory.ChangeNewsStatus(nid);
            else if(act=="del")
                NewsFactory.DeleteNews(nid);
            return RedirectToAction("Index");
        }


        public ActionResult Setup(int id)
        {
            UNews un = NewsFactory.GetNews(id);

            return View(un);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Setup(int id, string Ntitle, string Ncontent)
        {
            NewsFactory.UptNews(id, Ntitle, Ncontent);

            return RedirectToAction("Setup");
        }

        public ActionResult Add()
        {
            return View();
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Add(string Ntitle, string Ncontent)
        {
            NewsFactory.AddNews(Ntitle, Ncontent);

            return RedirectToAction("Index");
        }

        //儲存上傳檔案
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult ImgUpload(HttpPostedFileBase upload)
        {
            string TargetDir = "/upload/news";
            UploadImage.SaveUploadMessage Sr = new UploadImage.SaveUploadMessage();
            UploadImage uploadBase = new UploadImage();
            Sr = uploadBase.SaveUploadImage(upload, TargetDir, "", false, 300);

            ViewData["CKEditorFuncNum"] = Request.QueryString["CKEditorFuncNum"];
            ViewData["fileurl"] = Config.Site + "/upload/news/" + Sr.NewFileName;
            ViewData["msg"] = Sr.SaveErrorMessage;
            return View();
        }

    }
}
